﻿/*
 * Rappelz: Endless Odyssey - The first free open source Rappelz server emulator
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rappelz.GameServer.Network;
using System.Data.Common;
using System.Data;
using Rappelz.GameServer.Network.GameClient;

namespace Rappelz.GameServer.Database
{
    /// <summary>
    /// DBLoader_Quest
    /// </summary>
    public class DBLoader_Quest : IDBProc
    {
        GameDBManager db;
        public BaseObject m_CBObject = null;

        private enum DBFields : int
        {
            AccountId = 0,
        }

        public DBLoader_Quest(GameDBManager dbManager)
        {
            db = dbManager;
        }

        public void SetCB(BaseObject obj)
        {
            m_CBObject = obj;
        }

        public void RunCB()
        {
            if (m_CBObject != null)
                m_CBObject.onEndQuery();
        }

        public bool onProcess()
        {
            DbConnection conn = db.CreateConnection();

            GameContent.ClearQuestLink();
            LoadQuestData(conn);
            LoadQuestLinkData(conn);
            LoadRandomPoolData(conn);
            return true;

        }

        private bool LoadQuestData(DbConnection conn)
        {
            DbCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select * from QuestResource";
            cmd.CommandType = CommandType.Text;

            conn.Open();
            int count = 0;
            int i;
            try
            {
                DbDataReaderTypeCastHelper dr = new DbDataReaderTypeCastHelper(cmd.ExecuteReader());
                while (dr.Read())
                {
                    QuestBaseServer q = new QuestBaseServer();
                    int off = 0;
                    q.nCode = dr.GetInt32(off++);
                    q.nQuestTextId = dr.GetInt32(off++);
                    q.nSummaryTextId = dr.GetInt32(off++);
                    q.nStatusTextId = dr.GetInt32(off++);
                    int limit_begin_time = dr.GetInt32(off++);
                    int limit_end_time = dr.GetInt32(off++);
                    q.nLimitLevel = dr.GetInt32(off++);
                    q.nLimitJobLevel = dr.GetInt32(off++);
                    q.nLimitMaxLevel = dr.GetInt32(off++);
                    int limit_max_job_level = dr.GetInt32(off++);

                    int limit_deva = dr.GetByte(off++);
                    int limit_asura = dr.GetByte(off++);
                    int limit_gaia = dr.GetByte(off++);
                    int limit_fighter = dr.GetByte(off++);
                    int limit_hunter = dr.GetByte(off++);
                    int limit_magician = dr.GetByte(off++);
                    int limit_summoner = dr.GetByte(off++);

                    q.nLimitJob = dr.GetInt32(off++);
                    int job_depth = dr.GetByte(off++);
                    q.nLimitFavorGroupId = dr.GetInt32(off++);
                    q.nLimitFavor = dr.GetInt32(off++);
                    q.bIsRepeatable = dr.GetByte(off++) != 0;
                    q.nInvokeCondition = dr.GetInt32(off++);
                    q.nInvokeValue = dr.GetInt32(off++);
                    int time_limit_type = dr.GetByte(off++);
                    q.nTimeLimit = dr.GetInt32(off++);
                    q.nType = (QuestBase.QuestType)dr.GetInt32(off++);
                    for (i = 0; i < QuestBase.MAX_VALUE_NUMBER; ++i)
                    {
                        q.nValue[i] = dr.GetInt32(off++);
                    }
                    q.nDropGroupId = dr.GetInt32(off++);
                    q.nQuestDifficulty = dr.GetInt32(off++);
                    q.nFavorGroupId = dr.GetInt32(off++);
                    q.nHateGroupId = dr.GetInt32(off++);
                    q.nFavor = dr.GetInt32(off++);
                    q.nExp = dr.GetInt64(off++);
                    q.nJP = dr.GetInt32(off++);
                    q.nHuntaholicPoint = dr.GetInt32(off++);
                    q.nGold = dr.GetInt32(off++);
                    q.DefaultReward.nItemCode = dr.GetInt32(off++);
                    q.DefaultReward.nLevel = dr.GetInt32(off++);
                    q.DefaultReward.nQuantity = dr.GetInt32(off++);
                    for (i = 0; i < QuestBase.MAX_OPTIONAL_REWARD; ++i)
                    {
                        q.OptionalReward[i] = new QuestBase.Reward();
                        q.OptionalReward[i].nItemCode = dr.GetInt32(off++);
                        q.OptionalReward[i].nLevel = dr.GetInt32(off++);
                        q.OptionalReward[i].nQuantity = dr.GetInt32(off++);
                    }
                    for (i = 0; i < QuestBase.MAX_FOREQUEST; ++i)
                    {
                        q.nForeQuest[i] = dr.GetInt32(off++);
                    }
                    q.bForeCheckType = dr.GetByte(off++) != 0;
//                    q.nEndType = dr.GetInt32(off++);
                    q.strAcceptScript = dr.GetString(off++);
                    q.strClearScript = dr.GetString(off++);
                    q.strScript = dr.GetString(off++);

/*
                    q.unk1 = dr.GetInt32(off++);
                    q.unk2 = dr.GetInt32(off++);
                    q.nIsMagicPointQuest = dr.GetInt32(off++);
                    q.unk4 = dr.GetInt32(off++);
                    q.unk5 = dr.GetInt32(off++);
                    q.unk6 = dr.GetInt32(off++);
                    q.unk7 = dr.GetInt32(off++);
*/

                    if (limit_asura != 0)
                        q.LimitFlag |= 4;
                    if (limit_gaia != 0)
                        q.LimitFlag |= 8u;
                    if (limit_deva != 0)
                        q.LimitFlag |= 2u;
                    if (limit_hunter != 0)
                        q.LimitFlag |= 0x20u;
                    if (limit_fighter != 0)
                        q.LimitFlag |= 0x10u;
                    if (limit_magician != 0)
                        q.LimitFlag |= 0x40u;
                    if (limit_summoner != 0)
                        q.LimitFlag |= 0x80u;

                    Quest.RegisterQuestBase(q);

                    count++;
                }
            }
            catch (System.Data.SqlClient.SqlException e)
            {
                Globals.Log.Warning("Can't load QuestResource database, Sql string: {0}", e.Message);
            }
            catch (Exception ex)
            {
                Globals.Log.Error("DBLoader_Quest.LoadQuestData Exception: {0}", ex);
            }
            conn.Close();
            Globals.Log.Info("Total {0} Quests loaded.", count);
            return true;
        }

        private bool LoadQuestLinkData(DbConnection conn)
        {
            DbCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select * from QuestLinkResource";
            cmd.CommandType = CommandType.Text;

            conn.Open();
            int count = 0;
            try
            {
                DbDataReaderTypeCastHelper dr = new DbDataReaderTypeCastHelper(cmd.ExecuteReader());
                while (dr.Read())
                {
                    QuestLink ql = new QuestLink();
                    ql.nNPCId = dr.GetInt32(0);
                    ql.code = dr.GetInt32(1);
                    ql.bLF_Start = dr.GetByte(2) != 0;
                    ql.bLF_Progress = dr.GetByte(3) != 0;
                    ql.bLF_End = dr.GetByte(4) != 0;
                    ql.nStartTextId = dr.GetInt32(5);
                    ql.nInProgressTextId = dr.GetInt32(6);
                    ql.nEndTextId = dr.GetInt32(7);
                    GameContent.RegisterQuestLink(ql);
                    count++;
                }
            }
            catch (System.Data.SqlClient.SqlException e)
            {
                Globals.Log.Warning("Can't load QuestLinkResource database, Sql string: {0}", e.Message);
            }
            catch (Exception ex)
            {
                Globals.Log.Error("DBLoader_Quest.LoadQuestLinkData Exception: {0}", ex);
            }
            conn.Close();
            Globals.Log.Info("Total {0} QuestLinks loaded.", count);
            return true;

        }

        private bool LoadRandomPoolData(DbConnection conn)
        {
            return true;
            /*
            DbCommand cmd = conn.CreateCommand();
            cmd.CommandText = "select * from ChannelUserLimitResource";
            cmd.CommandType = CommandType.Text;

            conn.Open();
            int count = 0;
            try
            {
                DbDataReaderTypeCastHelper dr = new DbDataReaderTypeCastHelper(cmd.ExecuteReader());
                while (dr.Read())
                {
                    int group_id = dr.GetInt32(0);
                    int quest_target_id = dr.GetInt32(1);
                    int target_level = dr.GetInt32(2);
                    GameContent.RegisterRandomPoolInfo(group_id, quest_target_id, target_level);
                    count++;
                }
            }
            catch (Exception ex)
            {
                Globals.Log.Error("DBLoader_Quest.LoadRandomPoolData Exception: {0}", ex);
            }
            conn.Close();
            Globals.Log.Info("Total {0} RandomPoolData loaded.", count);
            return true;
            */
        }
    }
}
